package com.wjm.demo.test3.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TransServer {

    public static void main(String[] args) throws Exception {
//        test0();
        test1();
    }

    private static void test1() throws Exception {
        ServerSocket ss = new ServerSocket(30000);

        while (true) {
            Socket accept = ss.accept();
            String ip = ss.getInetAddress().getHostAddress();
            System.out.println(ip + ".......connected");
//            BufferedReader br = new BufferedReader(new InputStreamReader(accept.getInputStream()));
            OutputStream out =accept.getOutputStream();//自动刷新
            out.write("你好 收到".getBytes("UTF-8"));
            out.flush();

            out.close();
            accept.close();
        }
//        ss.close();
    }

    private static void test0() throws IOException {
        ServerSocket ss = new ServerSocket(10001);
        Socket accept = ss.accept();
        String ip = ss.getInetAddress().getHostAddress();
        System.out.println(ip + ".......connected");
        BufferedReader br = new BufferedReader(new InputStreamReader(accept.getInputStream()));
        PrintWriter pw = new PrintWriter(accept.getOutputStream(),true);//自动刷新
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
//            pw.print(line.toUpperCase()+"\r\n");
//            pw.flush();
            pw.println(line.toUpperCase());
        }
        accept.close();
        ss.close();
    }
}
